#pragma once
#include "Protolcol.hpp"
enum
{
    Div_zero,
    Mod_zero,
    Other_err
};
class ServerCal
{
public:
    ServerCal()
    {
    }

    Response CalculatorHelper(const Request &req)
    {
        Response res(0, 0);
        switch (req.oper)
        {
        case '+':
            res.result_ = req.x + req.y;
            break;

        case '-':
            res.result_ = req.x - req.y;
            break;

        case '*':
            res.result_ = req.x * req.y;
            break;

        case '/':
        {
            if (req.y == 0)
                res.code_ = Div_zero;
            else
                res.result_ = req.x / req.y;
        }
        break;
        case '%':
        {
            if (req.y == 0)
                res.code_ = Mod_zero;
            else
                res.result_ = req.x % req.y;
        }
        break;
        default:
            res.code_ = Other_err;
            break;
        }
        return res;
    }

    std::string Calculator(std::string& package)
    {
        std::string content;
        //Decode----"len"\n"x op y"\n
        bool r=Decode(package,&content);
        if(!r)
            return "";
        //Request 反序列化
        Request req;
        r=req.Deserialization(content);
        if(!r) return "";

        content="";
        Response res=CalculatorHelper(req);

        res.Serialization(&content);
       content= Encode(content);
        return content;
    }

    ~ServerCal()
    {
    }
};